//
//  SettingViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/23.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import Kingfisher
import Alamofire
import PKHUD
class SettingViewController: BaseViewController, BottomShadow, AddCornerUIRectCorner {
    @IBOutlet weak var phoneNumberLabel: UILabel!
    @IBOutlet weak var topView: UIView!
    @IBOutlet weak var blueView: UIView!
    @IBOutlet weak var exitButton: UIButton!
    @IBOutlet weak var countLable: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        titleLabel(text: "设置", "icon_setup")
        // Do any additional setup after loading the view.
    }
    
    func setUI() {
        addCornerWithView(aView: topView, aCorners: [.topLeft, .topRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        addCornerWithView(aView: blueView, aCorners: [.bottomLeft, .bottomRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        addCornerWithView(aView: exitButton, aCorners: [.topLeft, .topRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        phoneNumberLabel.text = MineInfoModel.share.userInfModel?.mobilePhone
        let count = fileSizeOfCache()
        countLable.text = "\(count)" + "M"
    }
    
    override func setNavigation() {
        
    }
    
    // MARK: Taps
    @IBAction func changePhoneNumberTap(_ sender: UITapGestureRecognizer) {
    }
    
    @IBAction func changePWTap(_ sender: UITapGestureRecognizer) {
        let changePwVC = ChangePwViewController(nibName: "\(ChangePwViewController.self)", bundle: nil)
        navigationController?.pushViewController(changePwVC, animated: true)
    }
    
    @IBAction func feedbackTap(_ sender: UITapGestureRecognizer) {
        let feedbackVC = FeedbackViewController(nibName: "\(FeedbackViewController.self)", bundle: nil)
        navigationController?.pushViewController(feedbackVC, animated: true)
        
    }
    
    @IBAction func contactCustomerServiceTap(_ sender: UITapGestureRecognizer) {
        let serviceVC = CustomerServiceViewController(nibName: "\(CustomerServiceViewController.self)", bundle: nil)
        self.navigationController?.pushViewController(serviceVC, animated: true)
    }
    
    @IBAction func aboutTap(_ sender: UITapGestureRecognizer) {
        let aboutVC = AboutViewController(nibName: "\(AboutViewController.self)", bundle: nil)
        self.navigationController?.pushViewController(aboutVC, animated: true)
    }
    
    @IBAction func clearCacheTap(_ sender: UIButton) {
        clearCache()
        KingfisherManager.shared.cache.clearMemoryCache()
        KingfisherManager.shared.cache.clearDiskCache(completion: nil)
        countLable.text = "0" + "M"
    }
    
    @IBAction func exitLoginTap(_ sender: UIButton) {
        let requesturl = "\(BaseURL)\(logoutUrl)"
        HUD.show(.label("注销中..."))
        Alamofire.request(requesturl, method: .post,
                          parameters: nil,
                          encoding: URLEncoding.default,
                          headers: ["token":MineInfoModel.share.token!]).responseJSON {
                            (response) in
                            HUD.hide()
                            self.showToast(msg:"注销成功")
                            MineInfoModel.share.exitLogin()
                            self.navigationController?.popViewController(animated: true)
                            
        }
        
    }
    
    func fileSizeOfCache()-> Int {
        
        // 取出cache文件夹目录 缓存文件都在这个目录下
        let cachePath: String = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
        //缓存目录路径
        print(cachePath)
        
        // 取出文件夹下所有文件数组
        let fileArr = FileManager.default.subpaths(atPath: cachePath)
        
        //快速枚举出所有文件名 计算文件大小
        var size = 0
        for file in fileArr! {
            
            // 把文件名拼接到路径中
            let path = cachePath + ("/\(file)")
            // 取出文件属性
            let floder = try! FileManager.default.attributesOfItem(atPath: path)
            // 用元组取出文件大小属性
            for (abc, bcd) in floder {
                // 累加文件大小
                if abc == .size {
                    size += bcd as! Int
                }
            }
        }
        
        let mm = size / 1024 / 1024
        
        return mm
    }
    func clearCache() {
        
        // 取出cache文件夹目录 缓存文件都在这个目录下
        let cachePath:String = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
        
        // 取出文件夹下所有文件数组
        let fileArr = FileManager.default.subpaths(atPath: cachePath)
        
        // 遍历删除
        for file in fileArr! {
            
            let path = cachePath + ("/\(file)")
            if FileManager.default.fileExists(atPath: path) {
                
                do {
                    try FileManager.default.removeItem(atPath: path)
                } catch {
                    
                }
            }
        }
    }
    
}
